﻿using System.Collections.Generic;
using WebAPI.Dao;
using WebAPI.Entities;

namespace WebAPI.Services;

public class UserService
{
    private UserMapper _userMapper = new UserMapper();
    private IdGenerate _idGenerate = new IdGenerate();
    
    public List<User> GetAllUser(string? username)
    {
        List<User> _users = _userMapper.SelectAll();
        
        return _users;
    }
    
    public User GetOne(string username)
    {
        User user = _userMapper.SelectOne(username);
        if (user != null)
        {
            user.Password = "";
        }
        
        return user;
    }
    public User Login(string username, string password)
    {
        password = EncryptDecrypt.DESEncrypt(password);
        User user = _userMapper.SelectOne(username);
        if (user.Password != password)
        {
            return null;
        }
        if (user != null)
        {
            user.Password = "";
        }
        return user;
    }

    public bool Register(User user)
    {
        user.Id = _idGenerate.GetId();
        user.Avatar = "";
        user.Email = "";
        user.Password = EncryptDecrypt.DESEncrypt(user.Password);
        user.CreateTime = TimeTransferUtils.GetSqlServerTimeString();
        user.UpdateTime = TimeTransferUtils.GetSqlServerTimeString();
        
        int flag = _userMapper.Insert(user);
        return flag > 0;
    }
    
    public bool UpdateUser(User user)
    {
        user.UpdateTime = TimeTransferUtils.GetSqlServerTimeString();
        int flag = _userMapper.Update(user);
        
        return flag > 0;
    }
    
    public bool DeleteUser(User user)
    {
        int flag = _userMapper.Delete(user);
        
        return flag > 0;
    }
}